let app = getApp();
let utils = require("../../utils/util.js");
let api = require("../../service/api.js");
let that;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    areas: ["华中", "华南", "华北", "华东", "东北", "西北", "西南"],
    areaIndex: 0,
    userInfo: {
      name: "",
      mobileNumber: "",
      branchCompanyName: "",
      answer: 0,
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let answer = options.cupsNumber;
    that = this;
    this.setData({
      ["userInfo.answer"]: answer || ''
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  uploadInfo() {
    let params = {
      wxUserName: that.data.userInfo.wxUserName,
      gender: that.data.userInfo.gender == 1 ? '男' : '女',
      avatarUrl: that.data.userInfo.avatarUrl,
      activityMaterialId: 1,
      name: that.data.userInfo.name,
      area: that.data.areas[that.data.index],
      branchCompanyName: that.data.userInfo.branchCompanyName,
      activityAnswer: that.data.userInfo.answer,
      phoneNumber: that.data.userInfo.mobileNumber,
    }
    if (!params.name || !params.area || !params.branchCompanyName || !utils.isPhoneNumber(params.phoneNumber) || !params.activityAnswer) {
      return wx.showToast({
        title: '请正确填写信息',
        icon: "none"
      });
    }
    wx.showLoading({
      title: '上传中...',
    })
    // if (wx.getStorageSync(app.globalData.userInfo.nickName)) {
    //   return wx.showToast({
    //     title: '不能重复参加活动',
    //     icon: "none"
    //   });
    // }
    api.WxRequest(api.saveParticipantInfo, params, (res) => {
      // wx.setStorageSync(app.globalData.userInfo.nickName, 1);
      wx.hideLoading();
      if (res.status) {
        wx.showToast({
          title: '参加成功',
          icon: "none"
        });
      }
    })
  },
  bindAreaPickerChange(e) {
    this.setData({
      index: e.detail.value
    });
  },
  bindInput(e) {
    let key = e.target.dataset.key;
    let value = e.detail.value;
    if (value) {
      that.setData({
        ['userInfo.' + key]: value
      })
    }
  },
  getLoginCode(cb) {
    return new Promise(function (resolve, reject) {
      wx.login({
        success: res => {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          resolve(res.code);
        },
        fail() {
          reject(-1)
        }
      })
    })
  },
  // 授权获取个人头像,信息
  getUserInfo: function (e) {
    app.globalData.userInfo = e.detail.userInfo
    that.setData({
      "userInfo.wxUserName":e.detail.userInfo.nickName,
      "userInfo.gender":e.detail.userInfo.gender,
      "userInfo.avatarUrl": e.detail.userInfo.avatarUrl
    });

    that.uploadInfo();
  },
})
